package cn.famouscat.ex003;

import android.content.Context;
import android.graphics.*;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.Scroller;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by huangjin.
 * 2016/01/22
 */
public class MyViewLine extends View {

    private int[] mColors = {
            0xFFCCFF00, 0xFF6495ED,
            0xFFE32636, 0xFF800000,
            0xFF808000, 0xFFFF8C69,
            0xFF808080, 0xFFE6B800,
            0xFF7CFC00};

    private Scroller mScroller;

    private Context mContext;

    private List<Line> lineList;

    private Paint mPaint;

    private float mWidth;
    private float mHeight;

    private float mR;
    private RectF mRectF;

    private int mMax;

    public MyViewLine(Context context) {
        super(context);
        init(context);
    }

    public MyViewLine(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public MyViewLine(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context){
        this.mContext = context;
        mScroller = new Scroller(mContext);
        mPaint = new Paint();

        mPaint.setAntiAlias(true);
//        mPaint.set

    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);

        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);

        int width = 0;
        int height = 0;

        switch (widthMode){
            case MeasureSpec.EXACTLY:{
                width = widthSize;
            }break;
            case MeasureSpec.AT_MOST:{
                width = 400;
            }break;
        }

        switch (heightMode){
            case MeasureSpec.EXACTLY:{
                height = heightSize;
            }break;
            case MeasureSpec.AT_MOST:{
                height = 400;
            }break;
        }



        setMeasuredDimension(width,height);
    }





    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

//        canvas.

        if(lineList == null){
            return;
        }
        canvas.setDrawFilter(new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG|Paint.FILTER_BITMAP_FLAG));
//        canvas.translate(mWidth/2,mHeight/2);

        float width = mWidth*0.8f;
        float height = mHeight*0.8f;

        float dividerWidth = width/(lineList.size()+1);

        canvas.translate(mWidth*0.1f,mHeight*0.9f);

//        canvas.translate((mWidth-mR)/2,mR);
//        mRectF = new RectF(-mR,-mR,mR,mR);
        Log.d("tag","=========width:"+width+"::::height:"+height);

        int size = lineList.size();

        for(int i = 0;i<size;i++){

            Line line = lineList.get(i);
            if(i >0){
                Line lastLine = lineList.get(i-1);
                mPaint.setColor(Color.WHITE);

                canvas.drawLine(dividerWidth*lastLine.pointX,-height/mMax*lastLine.pointY,dividerWidth*line.pointX,-height/mMax*line.pointY,mPaint);
            }

            mPaint.setColor(line.fillColor);
            Log.d("tag","=========pointX:"+line.pointX+"::::pointY:"+line.pointY);
            canvas.drawCircle(dividerWidth*line.pointX,-height/mMax*line.pointY,10,mPaint);
        }

    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mWidth = w;
        mHeight = h;
    }



    public void setData(int[]  data){
        if(data == null || data.length == 0){
            return;
        }
        lineList = new ArrayList<>();
        int max = 0;
        for(int i=0;i<data.length;i++){
            if(data[i]<=0){
                continue;
            }
            if(max < data[i]){
                max = data[i];
            }
            Line line = new Line();
            int j = i % mColors.length;       //设置颜色
            line.fillColor = mColors[j];
            line.pointY = data[i];
            line.pointX = i+1;
            lineList.add(line);

        }

        mMax = max+5-max%5;



        invalidate();
    }

    private class Line{
        public float pointX;
        public float pointY;
        public int fillColor;
    }
}
